<template>
  <div class="loginContainer">
    <el-form ref="loginForm" class="loginForm" :model="loginForm" :rules="rules"
             auto-complete="false">
      <el-form-item>
        <div class="loginTitle">
          系统登录
        </div>
      </el-form-item>
      <el-form-item prop="userName">
        <el-input
                id="userName"
                v-model="loginForm.userName"
                prefix-icon="el-icon-user-solid"
                placeholder="请输入用户名"
                name="userName"
                type="text"
                tabindex="1"
                auto-complete="false"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
                ref="password"
                id="password"
                v-model="loginForm.password"
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="false"
        />
      </el-form-item>

      <el-form-item >
        <el-row :gutter="20">
          <el-col :span="16" :offset="0">
            <el-input id="vertify"
                      v-model="loginForm.vertify"
                      prefix-icon="el-icon-question"
                      placeholder="请输入验证码"
                      prop="vertify"
                      @keyup.enter.native="submitLogin" />
          </el-col>
          <el-col :span="8" :offset="0">
            <img id="captchaImg" class="captchaImg" :src="captchaImg" @click="changeCaptcha">
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-button class="btn" type="primary" size="default" @click="submitLogin">登录</el-button>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-button class="btn" type="default" size="default">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        captchaImg:'',
        loginForm: {
          userName: 'esp',
          password: '123',
          vertify: '1qaz',
          token:''
        },
        rules:{
          userName: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          password:[{ required: true, message: '请输入密码', trigger: 'blur' }],
          vertify:[{ required: true, message: '请输入验证码', trigger: 'blur' }]
        }
      }
    },
    methods:{
     changeCaptcha(){
        this.$axios.get('/getCaptcha').then(res => {
          const { data } = res
          console.log(data)
          this.loginForm.token = res.data.data.token;
          this.captchaImg = res.data.data.captchaImg;
        }).catch((error) => {
          console.log('后端登录api请求失败！')
        })
      },
      submitLogin(){
        //组件自带方法 表单rules通过后
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.$axios.post('/userLogin').then(res => {
                const { data } = res
                console.log(data)
                const jwt = res.headers['authorization']
                this.$store.commit('SET_TOKEN',jwt)
                this.$router.replace('/home')
              }).catch((error) => {
                console.log('后端登录api请求失败！')
              })
          } else {
            this.$message.error("请输入所有字段")
            return false;
          }
        });
      }
    },
    created() {
      this.changeCaptcha();
    }
  }
</script>

<style  scoped>
  .loginContainer{
    border-radius: 10px;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background-color: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  .loginTitle{
    text-align: center;
    font-weight: 600;
    font-size: 24px;
  }
  .btn{
    width: 100%;
  }
  .captchaImg{
    width: 97px;
    height: 39px;
    cursor: pointer;
  }
</style>
